home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / STRCLASS.CPP < prev    next >
C/C++ Source or Header  |  1993-04-27  |  4KB  |  180 lines

  1. #include <string.h>
  2. #include <iostream.h>
  3. #include "strclass.h"
  4.  
  5. #define THIS    String
  6. #define BASE    Object
  7. DEFINE_CLASS(String, Object);
  8.  
  9. // 9/21/90
  10. void String::printOn(ostream& strm) const
  11. {
  12.     strm << str;
  13. }
  14.  
  15. //////////////////////////////////////////////////
  16. // constructors
  17. //////////////////////////////////////////////////
  18. String::String(const String &s)
  19. {
  20.     refs = s.refs;
  21.     (*refs)++;
  22.     iSize = s.iSize;
  23.     str = s.str;
  24. }
  25.  
  26. String::String(const char *s)
  27. {
  28.     refs = new int;
  29.     *refs = 1; // refs must be a pointer because
  30.                // two string objects might refer
  31.                // to the same int *.  A reference
  32.                // to either should update the SAME
  33.                // reference counter.  This would not
  34.                // be possible if each String object
  35.                // maintained a private reference
  36.                // count.
  37.     iSize = strlen(s);
  38.     str = new char[iSize + 1];
  39.     strcpy(str, s);
  40. }
  41.  
  42. String::String(const int length)
  43. {
  44.     iSize = length;
  45.     str = new char[iSize + 1];
  46.     str[0] = '\0';
  47.     refs = new int;
  48.     *refs = 1;
  49. }
  50.  
  51. //////////////////////////////////////////////////
  52. // destructor
  53. //////////////////////////////////////////////////
  54. String::~String()
  55. {
  56.     if (--(*refs) == 0)
  57.     {
  58.         delete str;
  59.         delete refs;
  60.     }
  61. }
  62.  
  63. //////////////////////////////////////////////////
  64. // assignment
  65. //////////////////////////////////////////////////
  66. String& String::operator=(const String &s)
  67. {
  68.     (*s.refs)++;
  69.     if (--(*refs) == 0)
  70.     {
  71.         delete refs;
  72.         delete str;
  73.     }
  74.     refs = s.refs;
  75.     iSize = s.iSize;
  76.     str = s.str;
  77.     return *this;
  78. }
  79.  
  80. String& String::operator=(const char *s)
  81. {
  82.     if (--(*refs) == 0)
  83.     {
  84.         delete refs;
  85.         delete str;
  86.     }
  87.     refs = new int;
  88.     *refs = 1;
  89.     iSize = strlen(s);
  90.     str = new char[iSize + 1];
  91.     strcpy(str, s);
  92.     return *this;
  93. }
  94.  
  95. //////////////////////////////////////////////////
  96. // comparison
  97. //////////////////////////////////////////////////
  98. int operator==(const String &s1, const String &s2)
  99. {
  100.     return strcmp(s1.str, s2.str)==0;
  101. }
  102.  
  103. int operator!=(const String &s1, const String &s2)
  104. {
  105.     return strcmp(s1.str, s2.str)!=0;
  106. }
  107.  
  108. int operator<(const String &s1, const String &s2)  // not in text
  109. {
  110.     return strcmp(s1.str, s2.str)<0;
  111. }
  112.  
  113. //////////////////////////////////////////////////
  114. // concatenation
  115. //////////////////////////////////////////////////
  116. String& operator+(const String &s1, const String &s2)
  117. {
  118.     String *s3 = new String(strlen(s1.str)+strlen(s2.str)+1);
  119.     strcpy(s3->str, s1.str);
  120.     strcat(s3->str, s2.str);
  121.     return *s3;
  122. }
  123.  
  124.  
  125. //////////////////////////////////////////////////
  126. // ostream output
  127. //////////////////////////////////////////////////
  128. ostream &operator<<(ostream &os, const String &s)
  129. {
  130.     return os << s.str;
  131. }
  132.  
  133. //////////////////////////////////////////////////
  134. // hash
  135. // -gmv 4/19/93
  136. //////////////////////////////////////////////////
  137. unsigned String::hash() const
  138. {
  139.     register unsigned h = 0;
  140.     char *s = str;
  141.  
  142.     
  143.     // while (i--) h^=(*vp++)->hash();
  144.     while (*s) h^=(*s++);
  145.     return h;
  146. }
  147.  
  148. //////////////////////////////////////////////////
  149. // isEqual
  150. // -gmv 4/19/93
  151. //////////////////////////////////////////////////
  152. bool String::isEqual(const Object& ob) const
  153. {
  154.     return ob.isSpecies(class_String) && *this==*(String*)&ob;
  155. }
  156.  
  157. //////////////////////////////////////////////////
  158. // compare
  159. // -gmv 4/19/93
  160. //////////////////////////////////////////////////
  161. int String::compare(const Object& ob) const
  162. {
  163.     return strcmp(str, ((String*)&ob)->str);
  164. }
  165.  
  166. //////////////////////////////////////////////////
  167. // toUpper, toLower
  168. // 4/26/93 -gmv
  169. //////////////////////////////////////////////////
  170. void String::toUpper() const
  171. {
  172.     strupr(str);
  173. }
  174.  
  175. void String::toLower() const
  176. {
  177.     strlwr(str);
  178. }
  179.  
  180.